package com.ssyc.student.activity;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.SpKeys;
import com.ssyc.common.bean.BusInfo;
import com.ssyc.common.utils.SPUtil;
import com.ssyc.common.utils.SnackbarUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.student.R;

/**
 * 学生端--宠物性别选择界面
 * Created by Administrator on 2018/4/19 0019.
 */

public class StudentPetGenderChooseActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout llMain;
    private ImageView mLvBoy;
    private ImageView mLvGirl;
    private Button mBtOK;
    private ImageView lvPet;
    //返回按钮
    private ImageView iv_back;

    //四种宠物类型的集合
    private int[] pets = {R.drawable.student_flicker_dog,R.drawable.student_flicker_cat,
            R.drawable.student_flicker_monster, R.drawable.student_flicker_panda};

    //用于记录当前选择的性别值,1是男生,2是女生
    private int sexType = -1;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.student_activity_pet_gendare_choose;
    }

    @Override
    protected void init() {
        showContent();
        //初始化视图
        initView();
    }

    /**
     * 初始化视图的方法
     */
    private void initView() {
        iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);
        llMain = findViewById(R.id.ll_main);
        mLvBoy = findViewById(R.id.lv_boy);
        mLvBoy.setOnClickListener(this);
        mLvGirl = findViewById(R.id.lv_girl);
        mLvGirl.setOnClickListener(this);
        mBtOK = findViewById(R.id.bt_OK);
        mBtOK.setEnabled(false);
        mBtOK.setOnClickListener(this);
        lvPet = findViewById(R.id.lv_pet);
        lvPet.setImageResource(pets[SPUtil.getInt(this, SpKeys.PETTYPE)-1]);
    }


    /**
     * 接受事件的方法
     *
     * @param busInfo
     */
    @Override
    public void busEvent(BusInfo busInfo) {
        super.busEvent(busInfo);
        //关闭页面
        if (null != busInfo && StudentEditPetNameActivity.PETFINISH.equals(busInfo.vpScoll)) {
            finish();
        }
    }

    /**
     * 页面控件点击事件
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.lv_boy) {
            //男宠物
            if (sexType == 1) {
                return;
            } else {
                mBtOK.setEnabled(true);
                mBtOK.setBackgroundResource(R.drawable.student_bt_ok_select);
                sexType = 1;
                mLvBoy.setImageResource(R.drawable.student_pet_boy_selected);
                mLvGirl.setImageResource(R.drawable.student_pet_girl_unselect);
            }
        } else if (id == R.id.lv_girl) {
            //女宠物
            if (sexType == 0) {
                return;
            } else {
                mBtOK.setEnabled(true);
                mBtOK.setBackgroundResource(R.drawable.student_bt_ok_select);
                sexType = 0;
                mLvBoy.setImageResource(R.drawable.student_pet_boy_unselect);
                mLvGirl.setImageResource(R.drawable.student_pet_girl_selected);
            }
        } else if (id == R.id.bt_OK) {
            //确定
            if (sexType == -1) {
                //证明还未选择性别
                SnackbarUtil.ShortSnackbar(llMain, "您还未选择宠物性别哦", 0).show();
            } else {
                //将选中的性别值保存到Sp中
                SPUtil.put(StudentPetGenderChooseActivity.this, SpKeys.PETGENDAR, sexType+"");
                UiUtils.startActivity(this, StudentEditPetNameActivity.class);
            }
        }else if(id == R.id.iv_back){
            //返回
            finish();
        }
    }
}
